package cn.sylinx.horm.example.section4;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.resource.ClasspathSqlResource;
import cn.sylinx.horm.resource.parse.DefaultSqlParser;
import cn.sylinx.horm.resource.parse.SqlParser;
import cn.sylinx.horm.util.Pair;
import cn.sylinx.horm.util.ParamMapBuilder;

@Profile("section4")
@Component
public class Section4 implements CommandLineRunner {

    private SqlClient sqlClient;

    public Section4(SqlClient sqlClient) {
        this.sqlClient = sqlClient;
    }

    @Override
    public void run(String... args) throws Exception {
        parseSystemSql(false);
        parseSystemSql(true);
    }

    void parseAviatorSql(boolean useInc) {
        String sql = SqlConsts.testAviatorExample;
        Map<String, Object> params = new ParamMapBuilder().put("id", 2L).put("useInc", useInc)
                .put("plsholder", "plsholder").build(); // 参数
        SqlParser sqlParser = new DefaultSqlParser();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.applyAviatorResource(sql), params);
        Object[] paramArray = p.getSecond();

        System.out.println("when useInc = " + useInc);
        System.out.println("sql -> " + p.getFirst());
        System.out.print("params -> ");
        if (paramArray != null) {
            for (Object pm : paramArray) {
                System.out.print(pm + ",");
            }
        }
        System.out.println();
        System.out.println();
    }

    void parseSystemSql(boolean useInc) {
        String sql = SqlConsts.testExample;
        Map<String, Object> params = new ParamMapBuilder().put("id", 2L).put("useInc", useInc)
                .put("plsholder", "plsholderValue").build(); // 参数
        SqlParser sqlParser = new DefaultSqlParser();
        Pair p = sqlParser.parseSql(ClasspathSqlResource.apply(sql), params);
        Object[] paramArray = p.getSecond();

        System.out.println("when useInc = " + useInc);
        System.out.println("sql -> " + p.getFirst());
        System.out.print("params -> ");
        if (paramArray != null) {
            for (Object pm : paramArray) {
                System.out.print(pm + ",");
            }
        }
        System.out.println();
        System.out.println();
    }

    void query() {
        Map<String, Object> params = new ParamMapBuilder().put("id", 2L).build(); // 参数
        List<QueryResultDTO> dataList = sqlClient.queryList(ClasspathSqlResource.apply(SqlConsts.testQuery), params,
                QueryResultDTO.class);
        System.out.println("dataList -> " + dataList);
    }

    void create() {

        Map<String, Object> params = new ParamMapBuilder().put("name", "name4").put("number", "number4").put("flag", 4)
                .build();
        Serializable pk = sqlClient.insertForRetrieval(ClasspathSqlResource.apply(SqlConsts.testInsert), params);
        System.out.println("create result -> " + pk);

        Map<String, Object> params1 = new ParamMapBuilder().put("name", "name3").put("number", "number3").put("eid", pk)
                .build();
        Serializable pk1 = sqlClient.insertForRetrieval(ClasspathSqlResource.apply(SqlConsts.testInsert), params1);
        System.out.println("create1 result " + pk1);
    }
}
